<?php

namespace app\front\model\info;

use app\common\exception\BaseException;
use app\common\model\info\Info as InfoModel;
use app\front\model\menu\Menu as MenuModel;

/**
 * 内容模型
 */
class Info extends InfoModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'app_id',
        'update_time'
    ];

    /**
     * 文章详情：HTML实体转换回普通字符
     */
    public function getContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }


    /**
     * 详情
     */
    public static function detail($info_id)
    {
        if (!$model = parent::detail($info_id)) {
            throw new BaseException(['msg' => '文章不存在']);
        }
        // 累积阅读数
        $model->where('info_id', '=', $info_id)->inc('click', 1)->update();
        return $model;
    }

    /**
     * 获取列表
     */
    public function getList($menu_id, $menu, $data)
    {

        $model = $this;
        if ($menu['mould_id'] == 4) {
            return self::getSingleDetail($menu_id);
        } elseif ($menu['mould_id'] == 5) {
            return [];
        }
        if ($menu_id > 0) {
            $arr = MenuModel::getSubMenuId($menu_id);
            $model = $model->where('menu_id', 'in', $arr);
        }
        $list = $model->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($data);
        foreach ($list as &$item){
            if (mb_strlen($item['title'],'UTF8') > 17) {
                $item['title'] = mb_substr($item['title'], 0, 17,'UTF8') . '...';
            }
            if (mb_strlen($item['desc'],'UTF8') > 95) {
                $item['desc'] = mb_substr($item['desc'], 0, 95,'UTF8') . '...';
            }
        }
        return $list;
    }

    /**
     * 获取下一篇
     */
    public function getNextDetail($info)
    {
        $model = $this;
        $detail = $model->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->where('info_id', '<', $info['info_id'])
            ->where('menu_id', '=', $info['menu_id'])
            ->where('sort', '>=', $info['sort'])
            ->find();
        return $detail;
    }

    /**
     * 获取上一篇
     */
    public function getLastDetail($info)
    {
        $model = $this;
        $detail = $model->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->where('info_id', '>', $info['info_id'])
            ->where('menu_id', '=', $info['menu_id'])
            ->where('sort', '<=', $info['sort'])
            ->find();
        return $detail;
    }

    /**
     * 获取列表
     */
    public function getSearchList($data)
    {
        $model = $this;
        if (isset($data['search']) && $data['search']) {
            $model = $model->where('title', 'like', '%' . $data['search'] . '%');
        } else {
            return [];
        }
        $list = $model->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($data);
        return $list;
    }

}